<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/5.4.5/css/swiper.css">
  <style>
    .home-banner{
      width: 500px;
      background-color: rgb(223, 185, 185);
      margin: 20px auto;
      overflow-x: hidden;
    }
    img{
      width: 100%;
    }
  </style>
</head>
<body>
  <div id="app">
    <home-page></home-page>
  </div>
  <script src="./vue.js"></script>
  <script src="./axios.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/5.4.5/js/swiper.js"></script>
  <script>
    const Home = {
      template: `
        <div>
          <h2 ref="title">这是home页</h2>  
          <div class="swiper home-banner">
            <div class="swiper-wrapper">
              <div 
                v-for="banner in banners"
                :key="banner.id"
                class="swiper-slide">
                  <img :src="banner.picUrl"/>
                </div>
             
            </div>
          </div>
        </div>
      `,
      data() {
        return {
          banners: [],
          // 在data中定义 实例初始值
          swiper: null
        }
      },
      methods: {
        fetchBanners(){
          axios.get('https://api.it120.cc/conner/banner/list').then(res => {
            if(res.data.code === 0) {
              this.banners = res.data.data    
            }
          })
        },
        // 2 methods封装初始化 函数
        initSwiper(){
          this.swiper = new Swiper('.home-banner', {
            autoplay: true,//可选选项，自动滑动
          })
        }
      },
      watch: {
        banners(){
          this.$nextTick(() => {
            this.swiper.update();
          })
        }
      },
      created () {
        this.fetchBanners();
      },
      mounted () {
        // 3 mounted中调用初始化函数
        this.initSwiper();
      }
     
    }
    Vue.component('HomePage', Home)
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })
  </script>
</body>
</html>